<?php

class Core_Model_Customer_Collection_Shop extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('customer/shop');
        $this->from('{t}customer_shop', 'a')
                ->select()->fields('a.*')->field('shop_name','shop_name')->closest()
//                , 'b.province_name', 'b.city_name', 'b.district_name', 'b.townstreet_name', 'b.road_name', 'd.shop_name', 'd.address', 'd.mobile', 'd.business_phone')
//                ->field('c.name3', 'industry_name')
//                ->field('d.id', 'poi_code')
//                ->closest()
                ->join()
                ->right('{t}foundation_poi', 'd')->on('a.poi_id', 'd.id')->endSelf()
//                ->inner('{t}foundation_road', 'b')->on('d.road_id', 'b.id')->endSelf()
//                ->inner('{t}foundation_industry', 'c')->on('d.industry_id', 'c.id')
                ->closest();
    }


    public function filterBy($name, $arg) {
        switch ($name) {
            case 'ids' :
                return $this->contain('a.id',':ids:i')->addParameter('ids', $arg);
            case 'status':
                return $this->eq('status',':status:s')->addParameter('status',$arg);
            case 'poi':
                return $this->where()->like('d.id', ':poi:s')->closest()->addParameter('poi', '%' . $arg . '%');
            case 'name':
                return $this->where()->like('a.shop_name', ':shopName:s')->closest()->addParameter('shopName', '%' . $arg . '%');
            case 'road':
                return $this->where()->like('b.road_name', ':roadName:s')->closest()->addParameter('roadName', '%' . $arg . '%');
            case 'industry':
                return $this->where()->like('c.industry_name', ':industryName:s')->closest()->addParameter('industryName', '%' . $arg . '%');
        }
        return $this;
    }

    public function sortBy($name, $dir) {
        switch ($name) {
            case 'poi_id':
                return $dir == 'asc' ? $this->asc('a.name') : $this->desc('a.name');
        }
        return parent::sortBy($name, $dir);
    }

}